﻿namespace JxcLite.Services;

[WebApi, Service]
class BaseDataService(Context context) : ServiceBase(context), IBaseDataService
{
    #region JxGoods
    public Task<PagingResult<GoodsInfo>> QueryGoodsesAsync(PagingCriteria criteria)
    {
        return Database.Query<JxGoods>(criteria).ToPageAsync<GoodsInfo>();
    }

    public Task<List<GoodsInfo>> GetGoodsesAsync()
    {
        return Database.Query<JxGoods>().Where(d => d.CompNo == CurrentUser.CompNo).ToListAsync<GoodsInfo>();
    }

    public async Task<Result> DeleteGoodsesAsync(List<GoodsInfo> infos)
    {
        if (infos == null || infos.Count == 0)
            return Result.Error(Language.SelectOneAtLeast);

        var database = Database;
        var oldFiles = new List<string>();
        var result = await database.TransactionAsync(Language.Delete, async db =>
        {
            foreach (var item in infos)
            {
                await db.DeleteFilesAsync(item.Id, oldFiles);
                await db.DeleteAsync<JxGoods>(item.Id);
            }
        });
        if (result.IsValid)
            AttachFile.DeleteFiles(oldFiles);
        return result;
    }

    public async Task<Result> SaveGoodsAsync(UploadInfo<GoodsInfo> info)
    {
        var database = Database;
        var model = await database.QueryByIdAsync<JxGoods>(info.Model.Id);
        model ??= new JxGoods();
        model.FillModel(info.Model);

        var vr = model.Validate(Context);
        if (vr.IsValid)
        {
            if (await database.ExistsAsync<JxGoods>(d => d.Id != model.Id && d.Code == model.Code))
                vr.AddError($"商品[{model.Code}]已存在！");
        }
        if (!vr.IsValid)
            return vr;

        var fileFiles = info.Files?.GetAttachFiles(nameof(JxGoods.Files), "GoodsFiles");
        return await database.TransactionAsync(Language.Save, async db =>
        {
            await db.AddFilesAsync(fileFiles, model.Id, key => model.Files = key);
            await db.SaveAsync(model);
            info.Model.Id = model.Id;
        }, info.Model);
    }
    #endregion

    #region JxPartner
    public Task<PagingResult<PartnerInfo>> QueryPartnersAsync(PagingCriteria criteria)
    {
        return Database.Query<JxPartner>(criteria).ToPageAsync<PartnerInfo>();
    }

    public async Task<Result> DeletePartnersAsync(List<PartnerInfo> infos)
    {
        if (infos == null || infos.Count == 0)
            return Result.Error(Language.SelectOneAtLeast);

        var database = Database;
        var oldFiles = new List<string>();
        var result = await database.TransactionAsync(Language.Delete, async db =>
        {
            foreach (var item in infos)
            {
                await db.DeleteFilesAsync(item.Id, oldFiles);
                await db.DeleteAsync<JxPartner>(item.Id);
            }
        });
        if (result.IsValid)
            AttachFile.DeleteFiles(oldFiles);
        return result;
    }

    public async Task<Result> SavePartnerAsync(UploadInfo<PartnerInfo> info)
    {
        var database = Database;
        var model = await database.QueryByIdAsync<JxPartner>(info.Model.Id);
        model ??= new JxPartner();
        model.FillModel(info.Model);

        var vr = model.Validate(Context);
        if (vr.IsValid)
        {
            if (await database.ExistsAsync<JxPartner>(d => d.Id != model.Id && d.Name == model.Name))
                vr.AddError($"{model.Type}名称已存在！");
        }
        if (!vr.IsValid)
            return vr;

        var fileFiles = info.Files?.GetAttachFiles(nameof(JxPartner.Files), "PartnerFiles");
        return await database.TransactionAsync(Language.Save, async db =>
        {
            await db.AddFilesAsync(fileFiles, model.Id, key => model.Files = key);
            await db.SaveAsync(model);
            info.Model.Id = model.Id;
        }, info.Model);
    }
    #endregion
}